﻿using NcfDB.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NcfDB
{
    public partial class CreateTableForm : Form
    {
        private string dbName;
        Table table = new Table();
        public CreateTableForm()
        {
            InitializeComponent();
        }
        public CreateTableForm(string dbName)
        {
            InitializeComponent();
            this.dbName = dbName;
        }

        private void CreateTableForm_Load(object sender, EventArgs e)
        {
            this.Text = dbName + " 新建表";
            this.init();
        }
        private void init()
        {
            this.FieldDataGridView.ColumnCount = 2;
            this.FieldDataGridView.ColumnHeadersVisible = true;
            this.FieldDataGridView.Columns[0].Name = "字段名";
            this.FieldDataGridView.Columns[1].Name = "字段长度";
            DataGridViewComboBoxColumn dcc = new DataGridViewComboBoxColumn();
            foreach (string type in Enum.GetNames(typeof(FieldType)))
            {
                dcc.Items.Add(type);
            }
            dcc.Name = "字段类型";
            this.FieldDataGridView.Columns.Add(dcc);
            DataGridViewCheckBoxColumn Keydck = new DataGridViewCheckBoxColumn();
            Keydck.Name = "主键";
            this.FieldDataGridView.Columns.Add(Keydck);
            DataGridViewCheckBoxColumn Nulldck = new DataGridViewCheckBoxColumn();
            Nulldck.Name = "可为null";
            this.FieldDataGridView.Columns.Add(Nulldck);
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            if (this.NametextBox.Text != null && this.FieldDataGridView.Rows.Count <= 1)
            {
                MessageBox.Show("缺少表名或字段！");
                return;
            }
            else
                this.setTable();
            this.Close();
        }
        private void setTable()
        {
            this.table.Name = this.NametextBox.Text;
            for (int i = 0; i < this.FieldDataGridView.Rows.Count - 1; i++)
            {
                FieldProperty fp = new FieldProperty();
                fp.Name = this.FieldDataGridView.Rows[i].Cells["字段名"].Value.ToString();
                fp.Type = (FieldType)Enum.Parse(typeof(FieldType), this.FieldDataGridView.Rows[i].Cells["字段类型"].Value.ToString(), true);
                fp.Length = int.Parse(this.FieldDataGridView.Rows[i].Cells["字段长度"].Value.ToString());
                if (this.FieldDataGridView.Rows[i].Cells["可为null"].Value != null)
                    fp.acceptNull = bool.Parse(this.FieldDataGridView.Rows[i].Cells["可为null"].Value.ToString());
                else
                    fp.acceptNull = false;
                if (this.FieldDataGridView.Rows[i].Cells["主键"].Value != null)
                    fp.isPrimaryKey = bool.Parse(this.FieldDataGridView.Rows[i].Cells["主键"].Value.ToString());
                else
                    fp.isPrimaryKey = false;
                table.addFieldProperty(fp);
            }
            CoreDBSystem.getDB(this.dbName).addTable(table);
            CoreDBSystem.getDB(this.dbName).save();
        }

        private void FieldDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.FieldDataGridView.Columns[e.ColumnIndex].Name == "主键")
            {
                this.FieldDataGridView.Rows[e.RowIndex].Cells["可为null"].Value = false;
            }
            if (this.FieldDataGridView.Columns[e.ColumnIndex].Name == "可为null")
            {
                this.FieldDataGridView.Rows[e.RowIndex].Cells["主键"].Value = false;
            }
        }
    }
}
